<?php
if (!defined('FosfoaSystem')) {
    exit(1);
}
class orderModel extends spModel
{
    var $pk = "orders_id"; // 每个唯一的标志，可以称为主键
    var $table = "order"; // 数据表的名称

    /**
     * 根据订单ID查询订单
     */
    function Detail($orders_id)
    {
        $condition = array('orders_id' => $orders_id);
        $arr = $this->find($condition);
        return $arr;
    }

    /**
     * 建立新的订单
     */
    function create_order($status_id, $payment, $shippingselect, $ip, $userarr,
        $address, $code2, $total, $Discount=0, $type = 'create')
    {
        //echo $this->userarr['customers_id'];
        //exit;
        if ($type == 'create') {
            if ($_SESSION['orders_id'] <> '') { //判断当前SESSION记录的订单号是否为空，不为空就删除老记录
                $conditions = array('orders_id' => $_SESSION['orders_id']);
                spClass('orderModel')->delete($conditions);
                spClass('orders_productsModel')->delete($conditions);
                spClass("orders_status_historyModel")->delete($conditions);
            }
            $orders_sn = 'F' . date('YmdHis');
            $orderarr = array(
                'customers_id' => $userarr['customers_id'] <> '' ? $userarr['customers_id'] : 0,
                'customers_name' => $address['customers_firstname'] . ' ' . $address['customers_lastname'],
                'customers_company' => $address['entry_company'],
                'customers_street_address' => $address['entry_street_address'] <> '' ? $address['entry_street_address'] :
                    '0',
                'customers_suburb' => $address['entry_suburb'],
                'customers_city' => $address['entry_city'],
                'customers_postcode' => $address['entry_postcode'],
                'customers_state' => $address['entry_state'],
                'customers_country' => $address['entry_country_id'],
                'customers_telephone' => $address['customers_telephone'],
                'customers_email_address' => $userarr['customers_email_address'],

                'delivery_name' => $address['customers_firstname'] . ' ' . $address['customers_lastname'],
                'delivery_company' => $address['entry_company'],
                'delivery_street_address' => $address['entry_street_address'],
                'delivery_suburb' => $address['entry_suburb'],
                'delivery_city' => $address['entry_city'],
                'delivery_postcode' => $address['entry_postcode'],
                'delivery_state' => $address['entry_state'],
                'delivery_country' => $address['entry_country_id'],

                'billing_name' => $address['customers_firstname'] . ' ' . $address['customers_lastname'],
                'billing_company' => $address['entry_company'],
                'billing_street_address' => $address['entry_street_address'],
                'billing_suburb' => $address['entry_suburb'],
                'billing_city' => $address['entry_city'],
                'billing_postcode' => $address['entry_postcode'],
                'billing_state' => $address['entry_state'],
                'billing_country' => $address['entry_country_id'],
                'payment_method' => $payment,
                'shipping_method' => $shippingselect[1],
                'shipping_module_code' => $shippingselect[2],
                'date_purchased' => time(),
                'currency' => $code2['code'],
                'currency_value' => $code2['value'],
                'order_total' => $total['price2'] * $code2['value'],
                'order_discount'=> $Discount,//已经转换了汇率的价格
                'currency_left' => $code2['symbol_left'],
                'ip_address' => $ip,
                'orders_sn' => $orders_sn,
                'order_tax' => TAX,
                'orders_status' => $status_id);
            
            $orders_id = $this->create($orderarr); //生成订单
            $_SESSION['orders_id'] = $orders_id; //记录当前订单号
            $_SESSION['orders_sn'] = $orders_sn; //记录当前订单号
            //setcookie('orders_id', $orders_id);
            //setcookie('orders_sn', $orders_sn);
            $order_status_history = array(
                'orders_id' => $orders_id,
                'orders_sn' => $orders_sn,
                'orders_status_id' => $status_id, //此处的103是rppay支付接口的默认状态
                'customer_notified' => 1,
                'comments' => $_SESSION['comments'],
                'date_added' => time());
            spClass('orders_status_historyModel')->Create($order_status_history); //订单的支付交易过程历史记录
        }

        $cart = spClass("CartAPI");
        $tmpArray = array();
        $tmpArray = $cart->CartView(); // 防止在此之前数据被外部修改
        //dump($tmpArray);
        // price1 是产品1的总价格
        // price2 是产品2的总价格
        // price3 是产品3的总价格
        // count  是产品的总数量
        $i = 0;
        if (is_array($tmpArray['id'])) {
            foreach ($tmpArray['id'] as $key => $val) {
                $product = spClass("productsModel")->Detail($tmpArray['pid'][$key]);
                $priceview = Get_price(array('pid'=>$tmpArray['pid'][$key],'price'=>$tmpArray['price1'][$key],'price2'=>$tmpArray['price2'][$key],'strtype'=>1,'GDcart'=>5));
                $data = array(
                    'orders_id' => $_SESSION['orders_id'],
                    'products_id' => $tmpArray['pid'][$key],
                    'products_name' => $tmpArray['name'][$key],
                    'products_quantity' => $tmpArray['count'][$key],
                    'product_size' => $tmpArray['size'][$key],
                    'products_price' => $tmpArray['price1'][$key] * $code2['value'],
                    'discounted_price' => $priceview,
                    'final_price' => $tmpArray['price1'][$key] * $code2['value'] * $tmpArray['count'][$key],
                    'final_priceview_price' => $priceview * $tmpArray['count'][$key],
                    'products_tax' => '0.0000');
                if ($type == 'create') {
                    spClass('orders_productsModel')->create($data);
                }
                $i++;
            }
        }
        return true;
    }
    /**
     * 返回购物车内容
     */
    function cartlist()
    {
        $cart = spClass("CartAPI");
        $tmpArray = array();
        $tmpArray = $cart->CartView(); // 防止在此之前数据被外部修改
        //dump($tmpArray);
        $outArray = array(); //一维数组
        // price1 是产品1的总价格
        // price2 是产品2的总价格
        // price3 是产品3的总价格
        // count  是产品的总数量
        $i = 0;
        if (is_array($tmpArray['id'])) {
            foreach ($tmpArray['id'] as $key => $val) {
                $product = spClass("productsModel")->Detail($tmpArray['pid'][$key]);
                $outArray[$i]['cartid'] = $tmpArray['id'][$key];
                $outArray[$i]['pid'] = $tmpArray['pid'][$key];
                $outArray[$i]['name'] = $tmpArray['name'][$key];
                $outArray[$i]['image'] = $product['products_image'];
                $outArray[$i]['size'] = $tmpArray['size'][$key];
                $outArray[$i]['price1'] = $tmpArray['price1'][$key];
                $outArray[$i]['price2'] = $tmpArray['price2'][$key];
                $outArray[$i]['other'] = $tmpArray['other'][$key];
                $priceview = Get_price(array('pid'=>$tmpArray['pid'][$key],'price'=>$tmpArray['price1'][$key],'price2'=>$tmpArray['price2'][$key],'strtype'=>1,'GDcart'=>5));
                $outArray[$i]['Total'] = $priceview * $tmpArray['count'][$key];
                $outArray[$i]['count'] = $tmpArray['count'][$key];
                $i++;
            }
        }
        return $outArray;
    }
    /**
     * 修改订单
     */
    function update_order($data, $orders_sn, $orders_id)
    {   
        if($orders_sn=='' and $orders_id=='') return;
        $condition = array('orders_sn' => $orders_sn);
        $this->update($condition, $data);
        $this->SaleAddCount($orders_id);
        return true;
    }
    /**
     * 销售记数
     */
    function SaleAddCount($orders_id)
    {
       $request = spClass('orders_productsModel')->return_list($orders_id);
       foreach ($request as $key => $val) {
            spClass('productsModel')->Saleadd($val['products_id']);
        }
        return true;
    }
}